package com.h3c.ptability.async;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.h3c.ptability.dto.PtTodoTaskDTO;
import com.h3c.ptability.entity.PtNoticeRecord;
import com.h3c.ptability.entity.PtTodoTask;
import com.h3c.ptability.entity.PtTodoTask;
import com.h3c.ptability.service.CommService;
import com.h3c.ptability.service.IPtTodoTaskService;
import com.h3c.ptability.service.ThreadPoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Slf4j
@Component
public class AsyncSendMessage {

    @Autowired
    CommService commService;
    @Resource
    private ThreadPoolService threadPoolService;
    @Autowired
    IPtTodoTaskService todoTaskService;

    public void sendMessage(List<PtNoticeRecord> message) {
        Thread thread = new Thread(() -> {
            commService.sendMessage(message);
        });
        threadPoolService.submitTask(thread);
    }

    public void sendMessage(PtNoticeRecord message, List<File> fileList) {
        Thread thread = new Thread(() -> {
            commService.sendMessage(message, fileList);
        });
        threadPoolService.submitTask(thread);
    }


    public void addOrUpdateTodoTask(Iterable<PtTodoTask> tasks) {
        Thread thread = new Thread(() -> {
            for (PtTodoTask i : tasks) {
                todoTaskService.addOrUpdateTodoTask(i);
                todoTaskService.sendTodoTaskNew(i);
            }

        });
        threadPoolService.submitTask(thread);
    }

    public void addOrUpdateTodoTaskWithPw(Iterable<PtTodoTaskDTO> tasks) {
        Thread thread = new Thread(() -> {
            for (PtTodoTaskDTO i : tasks) {
                todoTaskService.addOrUpdateTodoTask(JSONObject.parseObject(JSON.toJSONString(i), PtTodoTask.class));
                todoTaskService.sendTodoTaskNewWithPw(i);
            }

        });
        threadPoolService.submitTask(thread);
    }

}
